package com.example.minzhengtong.ui.add

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.example.minzhengtong.Const
import com.example.minzhengtong.HttpRequest
import com.example.minzhengtong.base.BaseA
import com.example.minzhengtong.data.LoginBean
import com.example.minzhengtong.databinding.ActivityLoginBinding
import com.example.minzhengtong.utils.LogUtil
import com.example.minzhengtong.utils.SpUtil
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * created by WY on 2021/6/1.
 */
class LoginA : BaseA() {

    companion object{
        fun enterThis(context: Context){
            val intent = Intent(context, LoginA::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var binding : ActivityLoginBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setToolBarVisiabale(false)
        ImmersionBar.with(this).titleBar(binding.titleBar).statusBarDarkFont(true).init()

        binding.login.setOnClickListener {
            val name = binding.name.text.toString()
            val pass = binding.pass.text.toString()
            if (name.isNullOrBlank()){
                showToast("请输入用户名")
                return@setOnClickListener
            }
            if (pass.isNullOrBlank()){
                showToast("请输入密码")
                return@setOnClickListener
            }
            HttpRequest().login(name,pass,this)
        }

    }

    fun loginSuccess(json : String){
        LogUtil.d("登录成功$json")
        val bean = Gson().fromJson(json,LoginBean::class.java)
        runOnUiThread {
            bean.apply {
                if (1 == status){
                    showToast("登录成功")
                    SpUtil.getInstance().putString(Const.TOKEN,data.token)
                    HttpRequest().detail(baseContext)
                    finish()
                }else{
                    showToast(msg)
                }
            }
        }
    }

}